package com.scau.service.impl;

import com.scau.dao.PaymentFlowMapper;
import com.scau.dto.ConstantClass;
import com.scau.exception.FileUtilException;
import com.scau.exception.GoodsStockException;
import com.scau.exception.OrderItemException;
import com.scau.handleException.ResultCode;
import com.scau.pojo.*;
import com.scau.dto.OrderInfo;
import com.scau.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class BrandControllerServiceImpl implements IBrandControllerService {

    @Autowired
    private ICompanyInfoService companyInfoService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private IEWalletService eWalletService;
    @Autowired
    private PaymentFlowMapper paymentFlowMapper;


    @Override
    public CompanyInfo getCompanyInfo(String username){

        CompanyInfo companyInfo = companyInfoService.getCompanyInfo(username);

        return companyInfo;
    }

    @Override
    public List<Brand> getBrands(String username){
        List<Brand> brandList = brandService.getBrands(username);
        return brandList;
    }

    @Override
    public List<Goods> getGoods(String username) {

        return goodsService.getBrandGoodss(username);
    }

    @Override
    public Goods getUpdateGoods(String id){
        return goodsService.getBrandGoods(id);
    }

    @Override
    public List<OrderInfo> ordersManagement(String username) {

        List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();

        List<OrderItem> orderItemList = orderService.getBrandOrderItems(username);

        for(OrderItem orderItem : orderItemList){
            OrderInfo orderInfo = new OrderInfo(goodsService.getBrandGoods(orderItem.getGoodsId()),orderItem);
            orderInfoList.add(orderInfo);
        }

        return orderInfoList;
    }

    @Override
    public CompanyInfo saveCompanyInfo(CompanyInfo companyInfo) {
        int rows;
        //判断是否已添加过公司信息，若有则更新，没有则添加
        if(companyInfoService.getCompanyInfo(companyInfo.getUsername())!=null){
            //已存在公司信息，更新
            rows = companyInfoService.updateCompanyInfo(companyInfo);
        }else{
            //不存在，添加
            rows = companyInfoService.addCompanyInfo(companyInfo);
        }
        //判断受影响行数是否正确

        return companyInfo;
    }

    //品牌商-品牌管理
    //  --增加品牌
    @Override
    public void addBrand(Brand brand, MultipartFile logo) throws IOException {
        brandService.addBrand(brand,logo);
    }
    //  --跳转至修改品牌页面
    @Override
    public Brand getUpdateBrand(String id){
        Brand brand = brandService.getBrand(id);
        return brand;
    }
    //` --修改品牌
    @Override
    public void updateBrand(Brand brand,MultipartFile logo) throws IOException {
        brandService.updateBrand(brand,logo);
    }
    //  --删除品牌
    public void deleteBrand(String id) throws FileUtilException {
        brandService.deleteBrand(id);
    }

    //品牌商--商品管理
    //  --删除商品
    @Override
    public Goods deleteGoods(String id) throws FileUtilException {
        //添加事务管理

        Goods goods = goodsService.deleteBrandGoods(id);

        //返回被删除的商品信息
        return goods;
    }

    //  --更新商品状态
    @Override
    public Goods updateBrandGoodsStatus(Goods goods) {
        //添加事务管理

        int rows = goodsService.updateBrandGoodsStatus(goods);
        //判断rows受影响的行数


        return null;
    }

    //  --获得某张订单
    @Override
    public OrderInfo getBrandOrder(String id){
        OrderItem orderItem = orderService.getBrandOrderItem(id);
        Goods goods = goodsService.getBrandGoods(orderItem.getGoodsId());
        return new OrderInfo(goods,orderItem);
    }

    //  --更新订单状态
    @Override
    public OrderItem updateBrandOrderStatus(String id,Integer status) throws GoodsStockException, OrderItemException {
        OrderItem orderItem = orderService.getBrandOrderItem(id);
        orderItem.setStatus(status);
        System.out.println(orderItem);
        //更新订单状态
        orderService.updateOrderItem(orderItem);
        if(orderItem.getStatus()==5){
            //如果更新订单状态是取消订单
            //1.品牌商退回金额
            EWallet sellerEWallet = eWalletService.getWalletByUsername(orderItem.getSellerName());
            PaymentFlow paymentFlow = paymentFlowMapper.selectByPrimaryKey(orderItem.getId());
            double newBalance = sellerEWallet.getBalance()-paymentFlow.getAmountMoney();
            if(newBalance < 0.0){
                throw new OrderItemException(ResultCode.ORDER_CANCEL_ERROR);
            }
            sellerEWallet.setBalance(newBalance);
            eWalletService.updateWallet(sellerEWallet);
            //2.增加商品库存
            goodsService.updateBrandGoodsStock(orderItem.getGoodsId(),orderItem.getGoodsQty(), ConstantClass.INCREASE_GOODS);

            //3.借买方增加金额
            EWallet buyerEWallet = eWalletService.getWalletByUsername(orderItem.getBuyerName());
            buyerEWallet.setBalance(buyerEWallet.getBalance()+paymentFlow.getAmountMoney());
            eWalletService.updateWallet(buyerEWallet);
            //4.支付流水
            paymentFlowMapper.updateByPrimaryKey(paymentFlow);
        }

        return null;
    }

    private boolean judgeRows(int rows){
        if(rows != 1){
            //增删改操作影响行数不等于0，有异常，
            return false;
        }else{
            return true;
        }
    }


}
